<?php
//update lại tổng cọc, tổng tiền hiện tại cho toàn bộ khách hàng
include_once '../include/common.php';
$core = new core();

$list_customer_order = $core->db->get_results("SELECT * FROM zen_customer_order");
if($list_customer_order){
    $count=1;
    foreach($list_customer_order as $order){

        $data_total_order = $core->db->get_row("SELECT SUM(product_num*product_price) as total_vnd, SUM(product_num*store_price) AS total_pt,SUM(product_num*stamp_price) AS total_at
                                        FROM zen_customer_order_detail
                                        WHERE order_status !=6 AND customer_order_id=".$order->customer_order_id);
        $has_error = false;
        $total_order_money = 0;

        if($data_total_order->total_vnd==null){
            $data_total_order->total_vnd=0;
            $data_total_order->total_at=0;
            $data_total_order->total_pt=0;
        }else{
            switch($order->type_calculate_payment){
                case 'normal':
                    $total_order_money = $data_total_order->total_vnd;
                    break;
                case 'at':
                    $total_order_money = $order->exchange * $data_total_order->total_at;
                    break;
                case 'pt':
                    //tong PT + 5%PT + 12EU*weight
                    $total_order_money = $order->exchange*($data_total_order->total_pt + $data_total_order->total_pt*$order->service_percent/100 + $order->fee_per_kg*$order->weight);
                    break;
            }
        }

        $total_order_money = core::roundingMoney($total_order_money);

        if($total_order_money !=$order->total_order_money
        ){
            var_dump('debug='.($total_order_money - $order->total_order_money ));
            var_dump("don hang thu ".$count." bi sai voi customer_order_id =".$order->customer_order_id);
            var_dump(": total_order_money= ".$order->total_order_money." || total_money_vnd=".$total_order_money."<br/>");

            $has_error = true;
        }

        $data_total_order->total_at = core::roundingMoney($data_total_order->total_at,1);

        if(floatval($data_total_order->total_at) != floatval($order->total_stamp_price) ){
            var_dump("don hang thu ".$count." bi sai voi customer_order_id =".$order->customer_order_id);
            var_dump(": total_stamp_price= ".number_format($order->total_stamp_price)." || total_money_at=".number_format($data_total_order->total_at)."<br/>");
            $has_error = true;
        }


        $data_total_order->total_pt = core::roundingMoney($data_total_order->total_pt,1);

        if(floatval($data_total_order->total_pt) != floatval($order->total_store_price)
           ){
            var_dump("don hang thu ".$count." bi sai voi customer_order_id =".$order->customer_order_id);
            var_dump(": total_store_price= ".number_format($order->total_store_price)." || total_money_pt=".number_format($data_total_order->total_pt)."<br/>");
            $has_error = true;


        }

        if($has_error) {
            $count++;
            //sync data
            $total_current_money = $order->total_payment - $total_order_money ;
            $total_current_money = core::roundingMoney($total_current_money);

            $return_val =$core->db->query("UPDATE zen_customer_order SET
                total_order_money= $total_order_money, total_current_money= $total_current_money,
                total_store_price = $data_total_order->total_pt, total_stamp_price = $data_total_order->total_at
                    WHERE customer_order_id=$order->customer_order_id");


            var_dump("UPDATE zen_customer_order SET
                total_order_money= $total_order_money, total_current_money= $total_current_money,
                total_store_price = $data_total_order->total_pt, total_stamp_price = $data_total_order->total_at
                    WHERE customer_order_id=$order->customer_order_id");
            var_dump("result=".$return_val);
            echo "update success<br/>";
        }


    }
    
}
echo "DONE";
?>